﻿public bool ReadResponseAsString { get; set; }

protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Threading.CancellationToken cancellationToken)
{
    if (response == null || response.Content == null)
    {
{%- if GenerateNullableReferenceTypes -%}
        return new ObjectResponseResult<T>(default(T)!, string.Empty);
{%- else -%}
        return new ObjectResponseResult<T>(default(T), string.Empty);
{%- endif -%}
    }

    if (ReadResponseAsString)
    {
        var responseText = await ReadAsStringAsync(response.Content, cancellationToken).ConfigureAwait(false);
        try
        {
            var typedBody = {% if UseSystemTextJson %}System.Text.Json.JsonSerializer.Deserialize{% else %}Newtonsoft.Json.JsonConvert.DeserializeObject{% endif %}<T>(responseText, {% if UseRequestAndResponseSerializationSettings %}Response{% endif %}JsonSerializerSettings);
{%- if GenerateNullableReferenceTypes -%}
            return new ObjectResponseResult<T>(typedBody!, responseText);
{%- else -%}
            return new ObjectResponseResult<T>(typedBody, responseText);
{%- endif -%}
        }
        catch ({% if UseSystemTextJson %}System.Text.Json.JsonException{% else %}Newtonsoft.Json.JsonException{% endif %} exception)
        {
            var message = "Could not deserialize the response body string as " + typeof(T).FullName + ".";
            throw new {{ ExceptionClass }}(message, (int)response.StatusCode, responseText, headers, exception);
        }
    }
    else
    {
        try
        {
            using (var responseStream = await ReadAsStreamAsync(response.Content, cancellationToken).ConfigureAwait(false))
{%- if UseSystemTextJson -%}
            {
                var typedBody = await System.Text.Json.JsonSerializer.DeserializeAsync<T>(responseStream, {% if UseRequestAndResponseSerializationSettings %}Response{% endif %}JsonSerializerSettings, cancellationToken).ConfigureAwait(false);
{%- else -%}
            using (var streamReader = new System.IO.StreamReader(responseStream))
            using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(streamReader))
            {
                var serializer = Newtonsoft.Json.JsonSerializer.Create({% if UseRequestAndResponseSerializationSettings %}Response{% endif %}JsonSerializerSettings);
                var typedBody = serializer.Deserialize<T>(jsonTextReader);
{%- endif -%}
{%- if GenerateNullableReferenceTypes -%}
                return new ObjectResponseResult<T>(typedBody!, string.Empty);
{%- else -%}
                return new ObjectResponseResult<T>(typedBody, string.Empty);
{%- endif -%}
            }
        }
        catch ({% if UseSystemTextJson %}System.Text.Json.JsonException{% else %}Newtonsoft.Json.JsonException{% endif %} exception)
        {
            var message = "Could not deserialize the response body stream as " + typeof(T).FullName + ".";
            throw new {{ ExceptionClass }}(message, (int)response.StatusCode, string.Empty, headers, exception);
        }
    }
}